package com.example.hosptialtag.service;

import com.example.hosptialtag.pojo.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import java.util.List;
import java.util.Map;

/**
 * @Author liuyuhao&caiang
 * @Date 2022/2/21
 */

@Service
public class PatientService {
    @Autowired
    private final JdbcTemplate jdbcTemplate;
    @Autowired
    private CheckCardService checkCardService;
    private String sql;
    public PatientService(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public String bindCard(int patient_id, String patient_idcard , String patient_medicard, Model model){//添加病人，并根据身份证和医保添加标签
        if(patient_idcard.length() < 1 || patient_medicard.length() < 1){
            model.addAttribute("bc_message", "输入不能为空");
            return "patientDetail";
        }
        if(jdbcTemplate.queryForObject("select count(1) from tb_patient_info" +
                " where id = "+ patient_id, Integer.class)>0){
            model.addAttribute("bc_message", "该用户已绑定身份证和医保卡");
            return "patientDetail";
        }
        try {
            if (checkCardService.idcardValidate(patient_idcard) && checkCardService.medicardValidate(patient_medicard)) {//身份证和医保卡合法
                jdbcTemplate.update("insert into tb_patient_info(id, idcard, medicard) values(?,?,?)",
                        patient_id, patient_idcard, patient_medicard);
                if (checkCardService.Sex(patient_idcard)) {//=============性别==============
                    jdbcTemplate.update("insert into bind(bind_patient_id, bind_tag_id)\n" +
                            "values(?,?);", patient_id, 1);//女性
                } else {
                    jdbcTemplate.update("insert into bind(bind_patient_id, bind_tag_id)\n" +
                            "values(?,?);", patient_id, 2);//男性
                }
                if (checkCardService.isNoAdult(patient_idcard)) {//=============是否未成年=============
                    jdbcTemplate.update("insert into bind(bind_patient_id, bind_tag_id, bind_start_time, bind_end_time)\n" +
                            "values(?,?,now(),?);", patient_id, 3, checkCardService.adultTime(patient_idcard));//未成年（期限为出生日期+18），成年不用标签
                }
                if (checkCardService.isLocal(patient_medicard)) {//=============医保卡是否是本地=============
                    jdbcTemplate.update("insert into bind(bind_patient_id, bind_tag_id)\n" +
                            "values(?,?);", patient_id, 6);//本地
                }
            } else {
                model.addAttribute("bc_message", "输入身份证或医保卡不合格");
                return "patientDetail";
            }
        }
        catch (Exception e){
            System.out.println(patient_id+" "+patient_idcard+" "+patient_medicard);
        }
        model.addAttribute("bc_message", "绑定成功");
        return "patientDetail";
    }

    public String deletePatientById(int patient_id, Model model) {
        jdbcTemplate.update("update patient set patient_delete = 1 where patient_id = ?", patient_id);
        return "listPatient";
    }

    public String listAllPatient(Model model) {
        model.addAttribute("ListAllPatient", jdbcTemplate.queryForList("select * from tb_patient"));
        return "patientList";
    }

    public String getPatientById(int patient_id, Model model) {
        return jdbcTemplate.queryForMap("select * from tb_patient where id = ? and patient_delete != 1", patient_id).toString();
    }

    public String getPatientsCount(Model model){
        return (jdbcTemplate.queryForObject("select count(1) from tb_patient where patient_delete != 1", Integer.class)).toString();
    }
    public int getPatientsCountByPatientId(int patient_id){
        return jdbcTemplate.queryForObject("select count(1) from tb_patient where id = " + Integer.toString(patient_id), Integer.class);
    }

    public String getPatientListByCondition(List<String> list, Model model) {
        if(list.size() == 1 && list.get(0).equals("")){ //筛选条件为空
            sql = "select * from tb_patient p";
        }
        else {
            sql = "select * from tb_patient p\n" +
                    "where exists(\n" +
                    "select bind_patient_id\n" +
                    "from bind\n" +
                    "where 1 = 1 ";

            for (String t : list) {
                sql += "and exists(\n" +
                        "select * from bind\n" +
                        "where bind_patient_id = p.id and bind_tag_id =" + t + " ";
            }
            for (int i = 0; i < list.size(); i++) {
                sql += ")";
            }
            sql += ") ;";
        }
        model.addAttribute("ListAllPatient", jdbcTemplate.queryForList(sql));
        return "patientList";
    }


}